package routes

import (
	"gitee.com/kun_liu/to-log/app/controllers"
	"gitee.com/kun_liu/to-log/app/middleware"
	"github.com/gin-gonic/gin"
)

// RegisterRoutes 注册所有路由
func RegisterRoutes(r *gin.Engine) {
	// 初始化控制器
	homeController := controllers.NewHomeController()
	apiController := controllers.NewAPIController()
	adminController := controllers.NewAdminController()

	// 首页路由
	r.GET("/", homeController.Index)
	r.GET("/archive", homeController.Archive)
	r.GET("/category/:slug", homeController.Category)
	r.GET("/tag/:slug", homeController.Tag)
	r.GET("/post/:slug", homeController.Post)
	r.GET("/page/:slug", homeController.Page)

	// API路由组
	api := r.Group("/api")
	{
		// 用户相关API
		api.POST("/login", apiController.Login)

		// 文章相关API
		api.GET("/posts", apiController.GetPosts)
		api.GET("/posts/:id", apiController.GetPost)
		api.GET("/categories", apiController.GetCategories)
		api.GET("/tags", apiController.GetTags)
		api.POST("/comments", apiController.CreateComment)
	}

	// 后台路由组
	admin := r.Group("/admin")
	{
		// 登录和注册页面不需要认证
		admin.GET("/login", adminController.Login)
		admin.POST("/login", adminController.Login)
		admin.GET("/register", adminController.Register)
		admin.POST("/register", adminController.Register)

		// 需要认证的路由
		admin.Use(middleware.AdminAuth())
		{
			// 后台首页
			admin.GET("/", adminController.Dashboard)
			admin.GET("/logout", adminController.Logout)

			// 文章管理
			admin.GET("/posts", adminController.PostList)
			admin.GET("/post/new", adminController.PostEdit)
			admin.POST("/post/new", adminController.PostEdit)
			admin.GET("/post/edit/:id", adminController.PostEdit)
			admin.POST("/post/edit/:id", adminController.PostEdit)
			admin.GET("/post/delete/:id", adminController.PostDelete)

			// 页面管理
			admin.GET("/pages", adminController.PageList)
			admin.GET("/page/:id", adminController.PageEdit)
			admin.POST("/page/:id", adminController.PageEdit)

			// 评论管理
			admin.GET("/comments", adminController.CommentList)

			// 分类管理
			admin.GET("/categories", adminController.CategoryList)

			// 标签管理
			admin.GET("/tags", adminController.TagList)

			// 用户管理
			admin.GET("/users", adminController.UserList)
			// admin.GET("/users/:id", adminController.UserEdit)

			// 设置
			// admin.GET("/settings/general", adminController.SettingGeneral)
		}
	}

	// 404页面
	r.NoRoute(homeController.NotFound)
}
